package com.erp.erp_mask.controller;


import com.erp.erp_mask.pojo.TUsers;
import com.erp.erp_mask.service.TUsersService;
import com.erp.erp_mask.vo.Result;
import jakarta.annotation.Resource;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

/**
 * @BelongsProject: erp_mask
 * @BelongsPackage: com.erp.erp_mask.controller
 * @Author: wang
 * @CreateTime: 2024-05-08  11:20
 * @Description: TODO
 * @Version: 1.0
 * @effect:
 */
@RestController
@RequestMapping("/sys")
public class TUsersController {
    @Resource
    private TUsersService tUsersService;

    @GetMapping("/ceshi")
    public ModelAndView ceshi() {
        return new ModelAndView("/sys/ceshi");
    }

    @GetMapping("/to_login")
    public ModelAndView to_login() {
        return new ModelAndView("login");
    }

    @GetMapping("/user")
    public ModelAndView userindex() {
        return new ModelAndView("/sys/user");
    }

    //查询用户
    @GetMapping("/usersearch")
    public Result usersearch(String username, String name, Integer state, Long pageNo, Long pageSize) {
        return Result.success(tUsersService.searchPage(username, name, state, pageNo, pageSize));
    }

    //添加用户
    @PostMapping("/useradd")
    public Result addlist(@RequestBody TUsers tUsers) {
        BCryptPasswordEncoder PasswordEncoder = new BCryptPasswordEncoder();
        try {
            String encode = PasswordEncoder.encode(tUsers.getUserPassword());
            tUsers.setUserPassword(encode);
            tUsersService.Addlist(tUsers);
            return Result.success("添加成功!");
        } catch (Exception e) {
            return Result.fail("添加失败!");
        }
    }
    @PutMapping("/edit")
    public Result edit(@RequestBody TUsers tUsers){
        try{
            tUsersService.edit(tUsers);
            return Result.success("修改成功!");
        }catch (Exception e){
            return Result.fail("修改失败!");
        }
    }

    //删除用户
    @DeleteMapping("/del")
    public Result del(Integer id) {
        try {
            tUsersService.removeById(id);
            return Result.success("删除成功!");
        } catch (Exception e) {
            return Result.fail("删除失败!");
        }
    }
    //下拉列表查询
    @GetMapping("/searchList")
    public Result searchList(){
        return Result.success(tUsersService.list());
    }
}
